package com.example.laishichang.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import android.content.IntentFilter;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;




public class MainActivity extends AppCompatActivity {



    //  本地变量：监听网络变化
    private IntentFilter intentFilter;
    private NetworkChangeReceiver networkChangeReceiver;
    private EditText netStateTextView;  //  textView: 显示网络状态



    //  本地通知功能
    private IntentFilter localNotationIntentFilter;
    private LocalReceiver localReceiver;
    private LocalBroadcastManager localBroadcastManager;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        netStateTextView = (EditText)findViewById(R.id.text_NetworkState);

        //  创建网络监听需要的实例
        intentFilter = new IntentFilter();
        intentFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
        networkChangeReceiver = new NetworkChangeReceiver();
        //  注册网络监听事件
        registerReceiver(networkChangeReceiver, intentFilter);



        //  本地通知事件
        localBroadcastManager = LocalBroadcastManager.getInstance(this);
        // 获取实例
        Button button = (Button) findViewById(R.id.button_sendLocalNotation);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent("com.example.broadcasttest.LOCAL_BROADCAST");
                localBroadcastManager.sendBroadcast(intent); // 发送本地广播
            }
        });
        //  监听本地广播事件
        localNotationIntentFilter = new IntentFilter();
        localNotationIntentFilter.addAction("com.example.broadcasttest.LOCAL_BROADCAST");
        localReceiver = new LocalReceiver();
        localBroadcastManager.registerReceiver(localReceiver, localNotationIntentFilter);  // 注册本地广播监听器
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();

        //  注销监听事件
        unregisterReceiver(networkChangeReceiver);
    }





    /********************     监听网络变化   ********************/
    class NetworkChangeReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            ConnectivityManager connectionManager = (ConnectivityManager)
                    getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo networkInfo = connectionManager.getActiveNetworkInfo();

            if (networkInfo != null && networkInfo.isAvailable()) {
                Toast.makeText(context, "network is available",
                        Toast.LENGTH_SHORT).show();
                netStateTextView.setText("network: available", TextView.BufferType.NORMAL);
            } else {
                netStateTextView.setText("network: unavailable", TextView.BufferType.NORMAL);

                Toast.makeText(context, "network is unavailable",
                        Toast.LENGTH_SHORT).show();
            }
        }
    }



    /********************    发送本地通知   ********************/
    //  本地通知接收对象
    class LocalReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            Toast.makeText(context, "received local broadcast",
                    Toast.LENGTH_SHORT).show();
        }
    }

}
